package SwordFingerOffer;

import java.util.*;

public class Q32_PrintTreeLevel3 {
    //从上到下  之字打印二叉树


    public List<List<Integer>> levelOrderNoMap(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        boolean fromHead = true;
        while(!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            if (!fromHead){
                Collections.reverse(tmp);
            }
            res.add(tmp);
            fromHead = !fromHead;
        }
        return res;
    }

}
